package Pages;

import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;

import java.util.List;

import static Driver.DriverSingleton.*;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class HomePage extends BasePage {

    public HomePage goHomePage() {
        super.login();
        waitForWindow(2000);
        assertThat(driver.getTitle(), is("首页"));
        return this;
    }

    public WebDriver getDriver() {
        return this.driver;
    }

    /*
    1.修改密码
     */

    //修改密码
    public HomePage updatePassword(String oldPs, String newPs, String new2Ps) {
        //定位到气泡
        {
            WebElement element = driver.findElement(By.cssSelector(".el-popover__reference-wrapper"));
            Actions builder = new Actions(driver);
            builder.moveToElement(element).perform();
        }
        waitForWindow(2000);
        //选择修改密码
        driver.findElement(By.cssSelector(".menuActionItem > span:nth-child(1)")).click();
        waitForWindow(2000);
        //输入原密码
        driver.findElement(By.cssSelector(".el-row:nth-child(1) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(1) .el-input__inner")).sendKeys(oldPs);
        //输入新密码
        driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-input__inner")).sendKeys(newPs);
        //再次输入新密码
        driver.findElement(By.cssSelector(".el-row:nth-child(3) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-row:nth-child(3) .el-input__inner")).sendKeys(new2Ps);
        //点击确定
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();
        return this;
    }


    //失败错误提示1
    public String getUpdatePasswordInputErrorMessage1() {
        waitForWindow(2000);
        WebElement elementText = driver.findElement(By.cssSelector(".el-row:nth-child(1) .el-form-item__error"));
        String info = elementText.getText();
        return info;

    }

    //失败错误提示2
    public String getUpdatePasswordInputErrorMessage2() {
        waitForWindow(2000);
        WebElement elementText = driver.findElement(By.cssSelector(".el-row:nth-child(2) .el-form-item__error"));
        String info = elementText.getText();
        return info;
    }

    //失败错误提示3
    public String getUpdatePasswordInputErrorMessage3() {
        waitForWindow(2000);
        WebElement elementText = driver.findElement(By.cssSelector(".el-row:nth-child(3) .el-form-item__error"));
        String info = elementText.getText();
        return info;
    }

    /*
    2.会话检测
     */
    // 返回主页显示当前会话数
    public String getConversationNum() {
        //获取主页显示会话数
        WebElement element = driver.findElement(By.cssSelector(".el-col:nth-child(3) > span:nth-child(2)"));
        String num = element.getText();
        return num;
    }

    //返回主页实际当前会话数
    public String getConversationListNum() {
        List<WebElement> elements = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div[1]/div/ul/ul/li"));
        String cons = String.valueOf(elements.size());
        return cons;
    }

    //返回今日咨询数
    public String getTodayCstNum() {
        String num = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div[1]/div[1]/div[2]/div[1]/span[2]")).getText();
        return num;
    }

    //返回累计咨询数
    public String getAllCstNum() {
        String num = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div[1]/div[1]/div[1]/div[2]/span[2]")).getText();
        return num;
    }

    //返回今日咨询时间
    public String getTodayCstTime() {
        String num = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div[1]/div[1]/div[2]/div[2]/span[2]")).getText();
        return num;
    }

    //返回会话的状态
    public String getConversationState() {
        String state = driver.findElement(By.cssSelector(".name > span:nth-child(2)")).getText();
        return state;
    }

    //返回发起会话的访客姓名
    public String getConversationVisitor() {
        String state = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div/ul/ul/li/div/div[2]/span[1]")).getText();
        return state;
    }

    /*
    3.设置咨询上限
     */

    //打开咨询上限设置
    public HomePage openCstUpBoundSet() {
        //点击咨询设置
        driver.findElement(By.cssSelector(".referSet > span")).click();
        waitForWindow(2000);
        WebElement element = driver.findElement(By.cssSelector(".arrangeModal .dialogTitle > div"));
        assertThat(element.getText(), is("咨询上限"));
        return this;
    }

    //增加咨询上限
    public HomePage plusCstUpBound(){
        //click加号
        driver.findElement(By.cssSelector(".el-icon-plus")).click();
        return this;
    }

    //降低咨询上限
    public HomePage minusCstUpBound(){
        //click加号
        driver.findElement(By.cssSelector(".el-icon-minus")).click();
        return this;
    }

    //确认修改咨询上限
    public HomePage clickOKSetUpBound(){
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();
        return this;
    }

    //获取咨询设置咨询上限值
    public int getCstUpBoundNumFromSet(){
        waitForWindow(2000);
        WebElement element = driver.findElement(By.cssSelector("#app > div > div.content > div.homePage > div.el-row.el-row--flex > div.el-col.el-col-14 > div.el-dialog__wrapper > div > div.el-dialog__body > div > div > input"));
        int num = Integer.parseInt(element.getAttribute("aria-valuenow"));
        return num;
    }

    //获取首页咨询上限值
    public String getCstUpBoundNumFromHome(){
        waitForWindow(2000);
        WebElement element = driver.findElement(By.cssSelector(".consultCount"));
        String str = element.getText();
        return str;
    }

    //咨询上限设置消息提示
    public String getSetUpBoundMessage(){
        //如果成功则消息提示设置成功
        waitForWindow(2000);
        WebElement elementText = driver.findElement(By.cssSelector("div.el-message"));
        String info = elementText.getText();
        return info;
    }

    //咨询上限设置（键入咨询上限）
    public HomePage typeSetUpBound(String key){
        driver.findElement(By.cssSelector(".el-input__inner")).click();
        //键盘全选
        driver.findElement(By.cssSelector(".el-input__inner")).sendKeys(Keys.CONTROL,"a");
        //键盘删除
        driver.findElement(By.cssSelector(".el-input__inner")).sendKeys(Keys.BACK_SPACE);
        //键盘键入
        driver.findElement(By.cssSelector(".el-input__inner")).sendKeys(key);
        return this;
    }


    /*
    4.最近完成的咨询
     */

    //咨询师首页查看全部咨询记录
    public HomePage toRecordListPage(){
        driver.findElement(By.cssSelector(".el-col-12 > span")).click();
        waitForWindow(2000);
        assertThat(driver.getTitle(), is("咨询记录列表"));
        return this;
    }

    //咨询师首页第一条咨询记录查看详情
    public HomePage toRecordDetailPage(){
        driver.findElement(By.xpath("//button[contains(.,'查看详情')]")).click();
        waitForWindow(2000);
        assertThat(driver.getTitle(), is("咨询记录详情"));
        return this;
    }

    public void downloadRecord() {
        driver.findElement(By.xpath("//button[contains(.,'导出记录')]")).click();
        String info = getDialog();
        assertThat( info, containsString("确认导出") );

        driver.findElement(By.cssSelector(".el-button--small:nth-child(2) > span")).click();
        driver.findElement(By.cssSelector(".el-table__row:nth-child(1) .el-button:nth-child(1) > span")).click();

        waitForWindow(2000);
        System.out.println(driver.findElement(By.cssSelector(".content:nth-child(1)")).getText());
    }

    /*
    5.获取当前状态
     */
    public String getConsultantState(){
        WebElement element = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div[1]/div[1]/div[1]/div[1]/div/div[1]/div[3]"));
        return element.getText();
    }


}
